<?php
namespace app\member\model;

use app\BaseModel;
use app\massage\model\User;
use think\facade\Db;

class Integral extends BaseModel
{
    //会员积分表
    protected $name = 'longbing_card_v2_shop_integral_log';



    protected $append = [

        'create_time_text'
    ];


    /**
     * @author chenniang
     * @DataTime: 2021-11-04 13:57
     * @功能说明:
     */
    public function getCreateTimeTextAttr($value,$data){

        if(!empty($data['create_time'])){

            return date('Y-m-d H:i:s',$data['create_time']);

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-07-15 09:49
     * @功能说明:添加
     */
    public function integralAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-16 16:35
     * @功能说明:列表
     */
    public function integralList($dis,$page){

        $data = $this->where($dis)->order(['update_time desc','id desc'])->paginate($page)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                if($v['controller_type']==0){

                    $v['controller_name'] = '系统核算';

                    $icon = $v['integral_add']>0?'+':'-';

                    $integral_add = $v['integral_add']>0?$v['integral_add']:$v['integral_add']*-1;

                    $text = $v['integral_add']>0?'增加积分':'使用积分';

                    $boj    = $this->returnObj($v['type']);

                    $refund = $v['refund']==1?'(已退款)':'';

                    $v['text'] = $boj.$text.$icon.$integral_add.',现积分 '.$v['integral_after'].$refund;

                    $v['type_text'] = $boj;

                }

            }

        }
        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-14 15:17
     * @功能说明:返回一个对象
     */
    public function returnObj($type){

        switch ($type){
            //购买会员商品
            case 0:
                return '升级会员';

                break;
            //会员购买商品
            case 1:
                return '消费';

                break;
            //购买储值套餐赠送
            case 2:
                return '购买储值套餐';

                break;

            case 3:
                return '购买商城商品';

                break;

            case 4:
                return '购买餐饮菜品';

                break;

            case 5://取消订单
                return '商城商品退款';

                break;
            case 6://售后
                return '商城商品退款';

                break;
            case 7://
                return '同步银豹';

                break;
            case 8://
                return '报名活动';

                break;
            case 9://
                return '购买赛车产品';

                break;

            case 10://
                return '购买商城商品';

                break;
            case 11://
                return '购买餐饮商品';

                break;
            default:
                return '消费';

                break;
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:08
     * @功能说明:详情
     */
    public function integralInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:13
     * @功能说明:编辑
     */
    public function integralUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-31 10:28
     * @功能说明:增加用户积分
     */
    public function integralUserAdd($user_id,$integral,$uniacid,$status = 2,$type=0,$order_id=0){
        //积分倍率
        $arr = $this->pointDouble($user_id,$integral);

        $integral = $arr['integral'];
        //查询单日获取积分是否超限
        $integral = $this->dayGetIntegral($user_id,$integral,$uniacid);

        if(!empty(intval($integral))){

            $member_model = new User();

            $member_info  = $member_model->dataInfo(['id'=>$user_id]);

            $insert = [

                'uniacid' => $uniacid,

                'user_id' => $user_id,

                'integral_add'   => intval($integral),

                'integral_before'=> intval($member_info['integral']),

                'integral_after' => intval($member_info['integral']+$integral),

                'status'   => $status,

                'type'     => $type,

                'order_id' => $order_id,

                'double'   => $arr['double'],
            ];

            $res = $this->integralAdd($insert);

            if($res==0){

                return false;
            }

            $res = $member_model->dataUpdate(['id'=>$user_id],['integral'=>$insert['integral_after']]);

            if($res==0){

                return false;
            }


        }
        return true;

    }


    /**\
     * @author chenniang
     * @DataTime: 2021-11-12 17:31
     * @功能说明:积分倍率
     */
    public function pointDouble($user_id,$integral){

        $user_model = new User();

        $member_level = $user_model->where(['id'=>$user_id])->value('member_level');

        $arr['double'] = 1;

        $arr['integral'] = $integral;

        if(empty($member_level)){

            return $arr;
        }

        $level_model = new Level();

        $level = $level_model->levelInfo(['id'=>$member_level]);

        if(empty($level)){

            return $arr;
        }

        if(empty($level['integral_switch'])){

            return $arr;

        }

        $arr['double']   = $level['integral'];

        $arr['integral'] = floor($level['integral']*$integral);

        return $arr;


    }
    /**
     * @author chenniang
     * @DataTime: 2021-11-12 14:05
     * @功能说明:单天获得积分
     */
    public function dayGetIntegral($user_id,$integral,$uniacid){

        $config_model = new Config();

        $config = $config_model->configInfo(['uniacid'=>$uniacid]);

        if($config['integral_limit']==0){

            return $integral;
        }

        $dis[] = ['user_id','=',$user_id];

        $dis[] = ['integral_add','>',0];
        //单日获取积分
        $integral_day = $this->where($dis)->whereDay('create_time')->sum('integral_add');
        //单日可获取最大积分
        $integral_day_max = $config['integral_day_max'];

        $point = $integral_day_max-$integral_day;

        if($point<=0){

            return 0;
        }

        $point = $point-$integral>0?$integral:$point;

        return $point;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-04 10:21
     * @功能说明:每日领取的积分
     */
    public function dayIntegral($user_id,$uniacid){

        $config_model = new Config();

        $config = $config_model->configInfo(['uniacid'=>$uniacid]);

        if($config['integral_limit']==1){

            $dis = [

                'user_id' => $user_id,

                'refund'  => 0,

                'type'    => 1
            ];

            $integral    = $this->where($dis)->where('status','>',0)->whereDay('create_time')->sum('integral_add');

            $re_integral = $config['integral_day_max'] - $integral;

            $re_integral = $re_integral>0?$re_integral:0;

            return intval($re_integral);

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-04 11:07
     * @功能说明:给用户加积分
     */
    public function incUserIntegral($order_id,$user_id,$uniacid){

        $dis = [

            'order_id' => $order_id,

            'status'   => 1
        ];

        $info = $this->where($dis)->select()->toArray();

        $member_model = new Member();

        if(!empty($info)){

            foreach ($info as $value){

                $integral     = $value['integral_add'];

                $member_info  = $member_model->getMemberInfo(['user_id'=>$user_id,'uniacid'=>$uniacid]);

                $update = [
                    //修改状态
                    'status' => 2,
                    //当前
                    'integral_before' => intval($member_info['integral']),
                    //修改后对
                    'integral_after' => intval($member_info['integral']+$integral),

                ];

                $this->integralUpdate(['id'=>$value['id']],$update);

                if(is_numeric($integral)&&$integral>0){

                    $dis = [

                        'user_id' => $user_id

                    ];

                    $member_model->incDecGrowth($dis,$integral,'integral');

                }

            }

        }
        return true;

    }











}